import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:goumao_app/Customs/gm_app_util.dart';

import '../Model/gm_vip_model.dart';

class GMTouupHistoryScreen extends StatefulWidget {
  const GMTouupHistoryScreen({super.key});
  @override
// ignore: library_private_types_in_public_api
  _GMTouupHistoryScreenState createState() => _GMTouupHistoryScreenState();
}

class _GMTouupHistoryScreenState extends State<GMTouupHistoryScreen> {
  Future<List<GMVIPModel>> _gmHistoryData() async {
    String jsonString =
        await rootBundle.loadString('assets/mydata/gm_history.json');
    // 解析JSON数据
    List<GMVIPModel> data = (jsonDecode(jsonString) as List)
        .map((data) => GMVIPModel.fromJson(data))
        .toList();
    return data;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: const Color(0xffF8F8F8),
        appBar: AppBar(
          elevation: 0,
          title: GMAppUtil.navText("充值记录"),
        ),
        body: FutureBuilder<List<GMVIPModel>>(
          future: _gmHistoryData(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              return const CircularProgressIndicator(); // 显示加载指示器
            } else if (snapshot.hasError) {
              return Text('Error: ${snapshot.hasError}');
            } else {
              List<GMVIPModel> vipData = snapshot.data!;
              return SafeArea(
          child: ListView.builder(
              itemCount: vipData.length, // 列表总数
              itemBuilder: (context, index) =>
                  GMTouupHistoryItemListWidget(data: vipData[index],)),
        );
            }
          },
        ));
  }
}

class GMTouupHistoryItemListWidget extends StatelessWidget {
  final GMVIPModel data;
  const GMTouupHistoryItemListWidget({super.key, required this.data});
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          padding: const EdgeInsets.fromLTRB(16, 20, 16, 20),
          height: 94,
          color: const Color(0xffffffff),
          child: Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    data.name!,
                    style:
                        const TextStyle(color: Color(0xff000000), fontSize: 15),
                  ),
                  Text(
                    '+${data.price1.toString()}',
                    style: const TextStyle(
                        color: Color(0xff000000),
                        fontSize: 15,
                        fontWeight: FontWeight.w700),
                  ),
                ],
              ),
              const SizedBox(
                height: 12,
              ),
              Row(
                children: [
                  Text(
                    data.path!,
                    style:
                        const TextStyle(color: Color(0xff999999), fontSize: 11),
                  ),
                ],
              )
            ],
          ),
        ),
        const Divider(),
      ],
    );
  }
}
